package dev.alm.reggietakeoutservice.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import dev.alm.reggietakeoutservice.dto.UserPageQuery;
import dev.alm.reggietakeoutservice.entity.User;
import dev.alm.reggietakeoutservice.mapper.UserMapper;
import dev.alm.reggietakeoutservice.service.UserService;
import dev.alm.reggietakeoutservice.utils.Md5Util;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.security.NoSuchAlgorithmException;
import java.util.*;

@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Resource
    UserMapper userMapper;

    @Override
    public User addUser(User user) {
        String password = user.getPassword();
        String encrypt = null;
        try {
            encrypt = Md5Util.encrypt(password);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("加密失败");
        }
        user.setPassword(encrypt);
        int insert = userMapper.insert(user);
        if (insert > 0) {
            return user;
        } else {
            return null;
        }
    }

    @Override
    public User getUserById(Long id) {
        User user = userMapper.selectById(id);
        return user;
    }

    @Override
    public Boolean login(User user) {
        String username = user.getUsername();
        String password = user.getPassword();
        if (Strings.isEmpty(username) || Strings.isEmpty(password)) {
            return false;
        }
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        User user1 = userMapper.selectOne(wrapper);
        if (user1 == null) {
            return false;
        }
        try {
            Boolean decrypt = Md5Util.decrypt(password, user1.getPassword());
            if (!decrypt) {
                return false;
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public Map<String, Object> getUserListByConditionAndPage(UserPageQuery userPageQuery) {
        // 先查询符合条件的数据数量
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        String name = userPageQuery.getName();
        String username = userPageQuery.getUsername();
        String introduction = userPageQuery.getIntroduction();
        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(username)) {
            wrapper.like("username", username);
        }
        if (!StringUtils.isEmpty(introduction)) {
            wrapper.like("introduction", introduction);
        }
        int count = userMapper.selectCount(wrapper);
        log.info("查询到的数据数量为: {}", count);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", count);
        if (count == 0) {
            map.put("list", new ArrayList<Object>());
            return map;
        }
        // 分页查询数据
        List<User> userListByConditionAndPage = userMapper.getUserListByConditionAndPage(userPageQuery);
        map.put("list", userListByConditionAndPage);
        return map;
    }

    @Override
    public List<User> getUserList() {
        List<User> userList = userMapper.getUserList();
        return userList;
    }

}
